using System;
using Gtk;
using RaisingStudio.Data.Providers;
using System.Collections.Generic;
using RaisingStudio.Data.Entities;
using System.Data;
using UnitTest;
using GtkApplication1;

public partial class MainWindow : Gtk.Window
{
	DataContext dc;

	public MainWindow () : base(Gtk.WindowType.Toplevel)
	{
		Build ();
		
		this.dc = new DataContext ();
		this.dc.Log = Console.Out;
	}


	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}

	DataTable dataTable;

	protected virtual void mainWindow_DefaultActivated (object sender, System.EventArgs e)
	{
		//DataContext dc = new DataContext();
		//List<RaisingStudio.Data.Entities.IssueA> issueList = dc.QueryForList<RaisingStudio.Data.Entities.IssueA>();
		this.dataTable = this.dc.QueryForDataTable<IssueA> ();
		Console.WriteLine (this.dataTable.Rows.Count);
		
		this.datagrid1.DataSource = this.dataTable;
		
		this.datagrid1.DataBind ();
	}

	protected virtual void button1_Clicked (object sender, System.EventArgs e)
	{
		Console.WriteLine ("button1_Clicked");
//		
//		if(this.dataTable == null)
//		{
//			this.dataTable = this.dc.QueryForDataTable<IssueA>();
//			this.datagrid1.DataSource = this.dataTable;
		////			List<IssueA> issueList = this.dc.QueryForList<IssueA>();
		////			this.datagrid1.DataSource = issueList;
//			this.datagrid1.DataBind();
//		}
//		//MessageDialog messageDialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, (this.dataTable).Rows.Count.ToString());
//		//messageDialog.Show();
		RefreshData ();
	}

	public void RefreshData ()
	{
		this.dataTable = this.dc.QueryForDataTable<IssueA> (IssueA.All.OrderBy (IssueA._.IssueID));
		this.datagrid1.DataSource = this.dataTable;
		this.datagrid1.DataBind ();
	}

	protected virtual void buttonNew_Clicked (object sender, System.EventArgs e)
	{
		try
		{
//			DataRow dataRow = this.dataTable.NewRow();
//			dataRow[(string)IssueA._.CreateTime] = System.DateTime.Now;
//			dataRow[(string)IssueA._.Description] = "issue for test";
//			dataRow[(string)IssueA._.IssueType] = (int)IssueType.Issue;
//			dataRow[(string)IssueA._.Locked] = false;
//			dataRow[(string)IssueA._.Owner] = "me";
//			dataRow[(string)IssueA._.Progress] = 10;
//			dataRow[(string)IssueA._.Status] = (int)IssueStatus.New;
//			dataRow[(string)IssueA._.Title] = "issue title for test";
//			this.dataTable.Rows.Add(dataRow);
			
			IssueA issue = new IssueA ();
			issue.CreateTime = System.DateTime.Now;
			issue.Description = "Issue for UnitTest.";
			issue.IssueType = IssueType.Issue;
			issue.Locked = false;
			issue.Owner = "sa";
			issue.Progress = 10;
			issue.Status = IssueStatus.New;
			issue.Title = "test title " + this.dataTable.Rows.Count;
			this.dc.Insert<IssueA> (issue);
		}
		catch (Exception ex)
		{
			Console.WriteLine (ex);
		}
//		this.datagrid1.DataBind();		
		RefreshData ();
	}

	protected virtual void buttonEdit_Clicked (object sender, System.EventArgs e)
	{
		try
		{
			int issueID = GetSelectedIssueID ();
			IssueA issue = this.dc.GetData<IssueA> (issueID);
			
			EditingDialog editingDialog = new EditingDialog ();
			editingDialog.IssueID = issue.IssueID.ToString ();
			editingDialog.IssueTitle = issue.Title;
			editingDialog.IssueType = issue.IssueType != null ? (int)issue.IssueType + 1 : 0;
			editingDialog.IssueDescription = issue.Description;
			int dialogResult = editingDialog.Run();
			Console.WriteLine(dialogResult);
			if(dialogResult == (int)Gtk.ResponseType.Ok)
			{
				issue.Title = editingDialog.IssueTitle;				
				issue.IssueType = editingDialog.IssueType > 0 ? (IssueType?)(editingDialog.IssueType - 1) : null;
				issue.Description = editingDialog.IssueDescription;
				this.dc.Update<IssueA>(issue);
				
				RefreshData();
			}
		}
		catch (Exception ex)
		{
			Console.WriteLine (ex);
		}
	}

	public int GetSelectedIssueID ()
	{
		int selectedIndex = this.datagrid1.SelectedRow;
		int issueID = (int)this.dataTable.Rows[selectedIndex][(string)IssueA._.IssueID];
		return issueID;
		
	}

	protected virtual void buttonDelete_Clicked (object sender, System.EventArgs e)
	{
		int issueID = GetSelectedIssueID ();
		this.dc.Delete<IssueA> (issueID);
		RefreshData();
	}

	protected virtual void buttonSave_Clicked (object sender, System.EventArgs e)
	{
		try
		{
			this.dc.UpdateDataTable<IssueA> (this.dataTable);
		}
		catch (Exception ex)
		{
			Console.WriteLine (ex);
		}
	}
	
	
	
	
	
	
}
